home *** CD-ROM | disk | FTP | other *** search
- #ifndef LINT
- static char nixtimeid[]="@(#) nixtime.i 1.2 87/05/03 16:00:16";
- #endif /* LINT */
-
- /*
- Time handling routines for UNIX systems. These are included by the file
- machine.c as needed.
-
- The contents of this file are hereby released to the public domain.
-
- -- Rahul Dhesi 1986/12/31
- */
-
- struct tm *localtime();
-
- /*****************
- Function gettime() gets the date and time of the file handle supplied.
- Date and time is in MSDOS format.
- */
- gettime(handle,date,time)
- int handle, *date, *time;
- {
- struct stat buf; /* buffer to hold file information */
- struct tm *tm; /* will hold year/month/day etc. */
- if (fstat (handle, &buf) == -1) {
- prterror ('w', "Could not get file time\n");
- *date = *time = 0;
- } else {
- tm = localtime (&buf.st_mtime); /* get info about file mod time */
- *date = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
- ((tm->tm_year - 80) << 9);
- *time = tm->tm_sec / 2 + (tm->tm_min << 5) +
- (tm->tm_hour << 11);
- }
-
- }
-
- /*****************
- Function setutime() sets the date and time of the filename supplied.
- Date and time is in MSDOS format. It assumes the existence of a function
- gettz() that returns the the difference (localtime - gmt) in seconds.
- */
- int setutime(path,date,time)
- char *path;
- unsigned int date, time;
- {
- int year, month, day, hour, min, sec, daycount;
- long longtime;
- /* no. of days to beginning of month for each month */
- static int dsboy[12] = { 0, 31, 59, 90, 120, 151, 181, 212,
- 243, 273, 304, 334};
-
- /* part of following code is common to zoolist.c -- if memory
- space is tight, try to share the code */
- year = (((unsigned int) date >> 9) & 0x7f) + 1980;
- month = ((unsigned int) date >> 5) & 0x0f;
- day = date & 0x1f;
-
- hour = ((unsigned int) time >> 11)& 0x1f;
- min = ((unsigned int) time >> 5) & 0x3f;
- sec = ((unsigned int) time & 0x1f) * 2;
-
- #ifdef DEBUG
- printf (setutime: "year=%d month=%d day=%d hour=%d min=%d sec=%d\n",
- year, month, day, hour, min, sec);
- #endif
-
- /* Calculate days since 1970/01/01 */
- daycount = 365 * (year - 1970) + /* days due to whole years */
- (year - 1970) / 4 + /* days due to leap years */
- dsboy[month-1] + /* days since beginning of this year */
- day-1; /* days since beginning of month */
-
- if (year % 4 == 0 &&
- year % 400 != 0 && month >= 3) /* if this is a leap year and month */
- daycount++; /* is March or later, add a day */
-
- /* Knowing the days, we can find seconds */
- longtime = daycount * 24L * 60L * 60L +
- hour * 60L * 60L + min * 60 + sec;
-
- longtime = longtime + gettz(); /* adjust for timezone */
-
- /* special case: if MSDOS format date and time were zero, then we set
- time to be zero here too. */
- if (date == 0 && time == 0)
- longtime = 0;
-
- /* longtime is now the time of the file, in seconds, since
- 1970/01/01 00:00:00. Now we set both access and modification times */
- {
- long utimbuf[2];
- utimbuf[0] = utimbuf[1] = longtime;
- return (utime (path, utimbuf));
- }
- }
-